package com.fivetalks.client;

import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.WindowManager;

public class ArrowResultView extends SurfaceView implements Callback {

	Canvas canvas;
	Context mContext;
	SurfaceHolder mHolder;
	Bitmap arrow_blue;
	Bitmap arrow_red;
	Display display;

	long roomNumber;
	int displayWidth;
	int displayHeight;
	int start_x;
	int end_x;
	int size = 0;
	String sex;
	int from;
	int to;
	boolean isReady = true;
	boolean ready = true;
	static final int gap = 180; // 위에 설명 바의 크기

	Thread nThread;

	DrawThread mThread;

	public ArrowResultView(Context context, long roomNumber) {
		super(context);
		Log.d("여기는", "안왔다네");
		// TODO Auto-generated constructor stub
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		this.roomNumber = roomNumber;
		Log.d("룸넘버", roomNumber + "");
		mHolder = holder;
		mContext = context;

		display = ((WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		displayWidth = display.getWidth();
		displayHeight = display.getHeight() - gap;
		start_x = 0;
		end_x = displayWidth / 3;

		BitmapDrawable bd_blue = (BitmapDrawable)getResources().getDrawable(R.drawable.arrow_blue);
		BitmapDrawable bd_red = (BitmapDrawable)getResources().getDrawable(R.drawable.arrow_red);
		arrow_blue = bd_blue.getBitmap();
		arrow_red = bd_red.getBitmap();

		mThread = new DrawThread();
	}

	// SurfaceView의 크기가 바뀔때 호출
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	// SurfaceView가 만들어질때 호출
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mThread.start();

	}

	// SurfaceView가 종료될때 호출
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean done = true;
		while (done) {
			try {
				mThread.join();
				done = false;
			} catch (InterruptedException e) {

			}
		}
		mThread = null;
	}

	class DrawThread extends Thread {

		public void run() {
			SocketClient sc = SocketClient.getInstance(null, null, null, null,
					null, ArrowResultView.this, null, null);
			try {
				sc.getDos().writeUTF("getPartnerSelection");
				sc.getDos().writeUTF(roomNumber + "");
				Log.d("roomNumber", "요기 방번호는" + roomNumber);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// canvas = mHolder.lockCanvas();

			// try {
			// Log.d("여기까지", "시작이닷");
			// while(true){
			// if(size > 0)
			// break;
			// }
			// // for (int i = 0; i < size; i++) {
			// // Log.d("size", "size : " + size);
			// // // while(isReady){
			// // // Log.d("loop", "반복한다");
			// // // }
			// // Log.d("sex", sex+"");
			// // Log.d("from", from+"");
			// // Log.d("to", to+"");
			// // drawLines(sex, from, to);
			// // Log.d("그림", "그렸다");
			// // isReady = true;
			// // }
			// } catch (NumberFormatException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			// mHolder.unlockCanvasAndPost(canvas);
		}
	}

	public void goActivity(boolean couple) {

		while (ready) {

		}

		if (couple) {
			Log.d("arrow","아싸 커플");
			Intent intent = new Intent(mContext, LoveRoomActivity.class);

			intent.putExtra("roomNumber", roomNumber);

			mContext.startActivity(intent);
		} else {

			nThread = new Thread(new Runnable() {
				public void run() {

					SocketClient sc = SocketClient.getInstance(null, null,
							null, null, null, ArrowResultView.this, null,null);
					try {
						sc.getDos().writeUTF("roomOut");
						sc.getDos().writeUTF(roomNumber + "");
						Log.d("prechat", "roomOut 전송22222");
						sc.getDos().flush();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
					Intent intent = new Intent(mContext, MainFrameActivity.class);

					intent.putExtra("email", sc.getEmail());
					intent.putExtra("password", sc.getPassword());
					mContext.startActivity(intent);
				}
			});
			nThread.start();
		}
	}

	public void drawArrows(String sex, int from, int to) {
		int top = 0;
		
		if ("MALE".equals(sex)) {
			switch (from) {
			case 1:
				switch (to) {
				case 5:
					arrow_blue = imgRotate(arrow_blue, 0, 240);
					top = 100;
					break;
				case 6:
					break;
				case 7:
					break;
				case 8:
					break;
				}
				break;
			case 2:
				switch (to) {
				case 5:
					arrow_blue = imgRotate(arrow_blue, 305, 360);
					top = 240;
					break;
				case 6:
					break;
				case 7:
					break;
				case 8:
					break;
				}
				break;
			case 3:
				switch (to) {
				case 5:
					break;
				case 6:
					break;
				case 7:
					break;
				case 8:
					break;
				}
				break;
			case 4:
				switch (to) {
				case 5:
					break;
				case 6:
					break;
				case 7:
					break;
				case 8:
					break;
				}
				break;
			}
			canvas.drawBitmap(arrow_blue, start_x, top, null);
		} else {
			switch (from) {
			case 5:
				switch (to) {
				case 1:
					arrow_red = imgRotate(arrow_red, 0, 240);
					top = 130;
					break;
				case 2:
					arrow_red = imgRotate(arrow_red, 305, 360);
					top = 330;
					break;
				case 3:
					break;
				case 4:
					break;
				}
				break;
			case 6:
				switch (to) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				}
				break;
			case 7:
				switch (to) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				}
				break;
			case 8:
				switch (to) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				}
				break;
			}
			canvas.drawBitmap(arrow_red, start_x, top, null);
		}
	}

	public void drawLines(String sex, int from, int to) {

		int height = displayHeight / 8;
		int one_y = height;
		int two_y = height * 3;
		int three_y = height * 5;
		int four_y = height * 7;
		final int extra = 10;
		int from_y = 0;
		int to_y = 0;

		Paint pt = new Paint();
		pt.setStrokeWidth(10);

		if ("MALE".equals(sex)) {
			pt.setColor(Color.BLUE);
			Log.d("남자from", from + "");
			switch (from) {
			case 1:
				from_y = one_y;
				break;
			case 2:
				from_y = two_y;
				break;
			case 3:
				from_y = three_y;
				break;
			case 4:
				from_y = four_y;
				break;
			}
			Log.d("남자to", to + "");
			switch (to) {
			case 5:
				to_y = one_y;
				break;
			case 6:
				to_y = two_y;
				break;
			case 7:
				to_y = three_y;
				break;
			case 8:
				to_y = four_y;
				break;
			case 0:
				to_y = from_y;
			}

			canvas.drawLine(start_x, from_y, end_x, to_y, pt);

		} else {

			pt.setColor(Color.RED);
			Log.d("여자from", from + "");
			switch (from) {

			case 5:
				from_y = one_y + extra;
				break;
			case 6:
				from_y = two_y + extra;
				break;
			case 7:
				from_y = three_y + extra;
				break;
			case 8:
				from_y = four_y + extra;
				break;
			}
			Log.d("여자to", to + "");
			switch (to) {
			case 1:
				to_y = one_y + extra;
				break;
			case 2:
				to_y = two_y + extra;
				break;
			case 3:
				to_y = three_y + extra;
				break;
			case 4:
				to_y = four_y + extra;
				break;
			case 0:
				to_y = from_y;
			}
			canvas.drawLine(end_x, from_y, start_x, to_y, pt);
		}
	}

	private Bitmap imgRotate(Bitmap bmp, int degree, int width) {

		int mWidth = bmp.getWidth();
		int height = bmp.getHeight();

		Matrix matrix = new Matrix();
		matrix.postRotate(degree);

		Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, width, height, true);
		Bitmap rotatedBitmap = Bitmap.createBitmap(resizedBitmap, 0, 0, width, height, matrix, true);

		return rotatedBitmap;
	}
}